/*
 * drivechain.java
 *
 * Created on May 17, 2007, 6:36 PM
 *
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package drivechain;

import java.io.PrintStream;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.chain.ChainNode.MSG;
import tal.drivechain.system.ConfigManager;
import tal.drivechain.system.Globals;
import tal.drivechain.parsing.ParsingAid;
import tal.drivechain.plugin.Module;
import tal.drivechain.system.SharedOutputStream;
import tal.drivechain.system.Splasher;

/**
 * 
 * @author Tal Eisenberg
 */
public class drivechain {
    
    public static void main(String[] args)
    {
        // replace standard output
        SharedOutputStream out = new SharedOutputStream(System.out);

        // create splash screen
        Splasher splash = new Splasher();
        new Thread(splash).start();
        out.addStream(splash.getOutput());
        
        // save command
        String command = "java -jar drivechain.jar";
        for (String a : args)
            command += " " + a;
        Globals.setCommandLine(command);


        // do mac stuff
        if (Globals.getOS()==Globals.OS.Mac) {
            try {
                // Generate and register the OSXAdapter, passing it a hash of all the methods we wish to
                // use as delegates for various com.apple.eawt.ApplicationListener methods
                OSXListener l = new OSXListener();
                OSXAdapter.setQuitHandler(l, l.getClass().getDeclaredMethod("quit", (Class[])null));
                OSXAdapter.setAboutHandler(l, l.getClass().getDeclaredMethod("about", (Class[])null));
            } catch (Exception e) {
                System.err.println("Error while loading the OSXAdapter:");
                e.printStackTrace();
            }
        }

        PrintStream p = new PrintStream(out);
        // print version (and app name)
        p.println(ParsingAid.makeCaption(Globals.VERSION.toString()));

        ConfigManager conf = new ConfigManager(out);
        try {
            conf.addClassPath(); // add needed libraries to the classpath.
            conf.loadPackages(); // load module packages.
            conf.setConfigDefaults(); // load default settings from the configuration file.
        } catch (IllegalArgumentException ie) {
            p.println("Error: " + ie.getMessage());
            splash.destroy();
            return;
        }

        // read command line args and parse them.
        CommandLineParser parser = new CommandLineParser(args, conf, out);

        parser.log("\n** Compiling main chain\n", MSG.ANOUNCE);

        try {
            parser.createChain(); // create chain from command line options if any.
        } catch (IllegalArgumentException ie) {
            parser.log(ie.getMessage(), Module.MSG.ERROR);
            splash.destroy();
            return;
        } catch (AbortChainException a) {
            parser.log("Aborting chain construction: " + a.getMessage(), Module.MSG.ERROR);
            splash.destroy();
            return;
        }

        //starts the loop and waits until enter is sent to the console to exit.
        splash.destroy();
        parser.log("\n** Running drivechain controller\n", MSG.ANOUNCE);
        parser.getChainController().takeOver(); // pass control over to the controller. usually just keeps the process alive.
        System.exit(0);
    }

}


